package com.huawang.mdesign.model.service.controller;

import com.huawang.business.constants.ParamErrorConstants;
import com.huawang.business.dto.diagram.CreateNewNodeDTO;
import com.huawang.business.dto.diagram.MetaElementItem;
import com.huawang.business.dto.diagram.NewNodeDTO;
import com.huawang.business.dto.diagram.SelectedSimpleNavigation;
import com.huawang.business.dto.relation.RelationCriterionDTO;
import com.huawang.business.util.ValidateDataUtil;
import com.huawang.business.vo.ModelResultVo;
import com.huawang.business.vo.PropertyInfoVo;
import com.huawang.business.vo.diagram.RelationMapNodeVo;
import com.huawang.core.semantics.entity.script.ElementType;
import com.huawang.mdesign.model.service.inter.DiagramMapService;
import com.huawang.mdesign.model.service.vo.BaseResponse;
import com.huawang.mdesign.model.service.vo.PageResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@Tag(name = "关系图")
@RestController
@RequestMapping("/diagram/map")
public class DiagramMapController {

    @Autowired
    private DiagramMapService diagramMapService;

    @Operation(summary = "查询可选元素类型")
    @GetMapping("/queryMapElementTypes")
    public BaseResponse<List<ElementType>> queryMapElementTypes(String projectId, String mapId) {
        ValidateDataUtil.isNotEmpty(projectId);
        return BaseResponse.success(diagramMapService.queryMapElementTypes(projectId, mapId));
    }

    @Operation(summary = "查询可选 非抽象元素类型")
    @GetMapping("/queryElementTypesByNotAbstract")
    public BaseResponse<List<ElementType>> queryElementTypesByNotAbstract(String projectId) {
        ValidateDataUtil.isNotEmpty(projectId);
        return BaseResponse.success(diagramMapService.queryElementTypesByNotAbstract(projectId));
    }

    @Operation(summary = "查询追溯图节点树")
    @GetMapping("/queryMapTree")
    public BaseResponse<RelationMapNodeVo> queryMapTree(@Validated @NotNull(message =
            ParamErrorConstants.NO_PROJECT_ID) String projectId,
                                                        @Parameter(name = "追溯图id")
                                                        @NotNull(message = ParamErrorConstants.NO_INSTANCE_ID) String mapId,
                                                        String rootId,
                                                        @NotNull(message = ParamErrorConstants.NO_LEVEL) Integer level) {
        return BaseResponse.success(diagramMapService.queryMapTree(projectId, mapId, rootId, level));
    }

    @Operation(summary = "查询追溯图关系配置信息")
    @GetMapping("/queryRelationCriterion")
    public BaseResponse<RelationCriterionDTO> queryRelationCriterion(String projectId, String mapId) {
        return BaseResponse.success(diagramMapService.queryRelationCriterion(projectId, mapId));
    }

    @Operation(summary = "查询简单关系可选项")
    @GetMapping("/simple/relation/querySelectedList")
    public BaseResponse<List<SelectedSimpleNavigation>> querySelectedList(String projectId, String mapId,
                                                                          String type) {
        return BaseResponse.success(diagramMapService.querySelectedSimpleNavigation(projectId, mapId, type));
    }

    @Operation(summary = "查询链式关系可选元类或构造型")
    @GetMapping("/metachain/navigation/queryMetaElementList")
    public BaseResponse<List<MetaElementItem>> queryMetaElementList(String projectId,
                                                                    String mapId,
                                                                    @Parameter(name = "属性的key") String key,
                                                                    String type) {
        return BaseResponse.success(diagramMapService.queryMetaElementList(projectId, mapId, key, type));
    }

    @Operation(summary = "查询链式关系可选属性")
    @GetMapping("/metachain/navigation/queryProperty")
    public BaseResponse<List<PropertyInfoVo>> queryChainProperty(String projectId,
                                                                 String metaClassId,
                                                                 @Parameter(name = "diagram：追溯图；derivedPropertyExpression" +
                                                                         "：派生属性表达式；rule；变更分析规则") String type) {
        return BaseResponse.success(diagramMapService.queryChainProperty(projectId, metaClassId, type));
    }

    @Operation(summary = "查询链式关系是否能插入下一条链路")
    @GetMapping("/metachain/navigation/canInsertChain")
    public BaseResponse<Boolean> canInsertChain(String projectId, String key) {
        return BaseResponse.success(diagramMapService.canInsertChainByProperty(projectId, key));
    }

    @Operation(summary = "查询节点可创建的新节点列表")
    @GetMapping("/queryNewNodeList")
    public BaseResponse<List<NewNodeDTO>> queryNewNodeList(String projectId, String sourceId, String mapId) {
        return BaseResponse.success(diagramMapService.queryNewNodeList(projectId, sourceId, mapId));
    }

    @Operation(summary = "创建新节点")
    @PostMapping("/createNewNode")
    public BaseResponse<ModelResultVo> createNewNode(@RequestBody CreateNewNodeDTO createNewNodeDTO) {
        return BaseResponse.success(diagramMapService.createNewNode(createNewNodeDTO));
    }

    @Operation(summary = "通用查询新增节点列表")
    @GetMapping("/queryPlusMenuItem")
    public BaseResponse<List<NewNodeDTO>> queryPlusMenuItem(String projectId, String mapId, String modelId) {
        return BaseResponse.success(diagramMapService.queryPlusMenuItem(projectId, mapId, modelId));
    }

    @Operation(summary = "根据模型类型获取需要新增的节点")
    @GetMapping("/queryAvailableElements")
    public BaseResponse<PageResponseVo> queryAvailableElements(String projectId, String instanceId, String addWay, String addType, int page,
                                                               int size, String name) {
        ValidateDataUtil.isNotEmpty(projectId);
        return BaseResponse.success(diagramMapService.queryAvailableElements(projectId, instanceId, addWay, addType, page, size, name));
    }
}
